<template>
  <div class="top-category">
    <div class="container">
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="position:relative" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in list" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <goodItems></goodItems>
    </div>
  </div>
</template>

<script>
// 导入轮播图接口
import { findBanner } from '@/api/home'
// 导入 辅助函数
import { watch, ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
// 导入分类组件
import goodItems from './good-items.vue'
export default {
  name: 'category',
  components: {
    goodItems
  },
  setup (props) {
    const store = useStore()
    const route = useRoute()
    const router = useRouter()
    // 轮播图
    const sliders = ref([])
    findBanner().then(data => {
      sliders.value = data.result
    })
    const list = computed(() => {
      const _list = store.state.category.cateList.filter(
        v => v.id === route.params.id
      )
      if (_list.length) {
        return _list[0].children
      } else {
        return []
      }
    })

    watch(
      () => route.params.id,
      (to, from) => {
        if (to === 'undefined' || from === 'undefined') {
          router.back()
        }
      }
    )
    return { sliders, list }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }

  ::v-deep {
    .xtx-carousel .carousel-btn.prev {
      left: 20px;
    }
    .xtx-carousel .carousel-indicator {
      padding-left: 0;
    }
  }
}
</style>
